package authoritah.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class ParityView extends JFrame {
	private JMenuBar menuBar;
	
	private JMenu file;
	private JMenuItem mainMenu;
	private JMenuItem exit;

	private JMenu member;
	private JMenuItem memberList;
	private JMenuItem memberSpending;
	
	private JMenu event;
	private JMenuItem eventAttendance;
	private JMenuItem eventTypes;
	
	private JMenu report;
	private JMenuItem lottingRights;

	private JToolBar toolBar;
	private JPanel framePanel;

	private JPanel mainPanel;
	
	private CardLayout cardLayout;

	public ParityView(JPanel mainMenuView, JPanel memberMainView, JPanel eventAttendanceView, JPanel eventTypeView,
			JPanel spendView, JPanel lottingRightsView) {
		initComponents(mainMenuView, memberMainView, eventAttendanceView, eventTypeView, spendView, lottingRightsView);
		layoutComponents();
	}

	/**
	 * Initialises the components for display.
	 */
	private void initComponents(JPanel mainMenuView, JPanel memberMainView, JPanel eventAttendanceView, JPanel eventTypeView,
			JPanel spendView, JPanel lottingRightsView) {
		// Create and set up the window.
		setTitle("Authoritah");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		framePanel = new JPanel(new BorderLayout());

		cardLayout = new CardLayout();		
		mainPanel = new JPanel(cardLayout);
		mainPanel.add(mainMenuView, "Main Menu");
		mainPanel.add(memberMainView, "Member List");
		mainPanel.add(eventAttendanceView, "Event Attendance");
		mainPanel.add(eventTypeView, "Event Types");
		mainPanel.add(spendView, "Spent Points");
		mainPanel.add(lottingRightsView, "Lotting Rights");

		createMenus();
		createToolbar();
	}

	/**
	 * Creates and populates the Tool Bar.
	 */
	private void createToolbar() {
		toolBar = new JToolBar();
		toolBar.add(new JButton("Button"));
	}

	/**
	 * Creates and populates the Menu Bar
	 */
	private void createMenus() {
		// File Menu
		file = new JMenu("File");
		file.setMnemonic('F');
		mainMenu = createMenuItem("Main Menu", "", 'M', new ScreenChangeListener("Main Menu"));
		exit = createMenuItem("Exit", "", 'E', new ExitListener());
		file.add(mainMenu);
		file.add(exit);

		// Members Menu
		member = new JMenu("Member");
		member.setMnemonic('M');
		memberList = createMenuItem("Member List", "", 'L', new ScreenChangeListener("Member List"));
		memberSpending = createMenuItem("Member Spending", "", 'L', new ScreenChangeListener("Spent Points"));
		member.add(memberList);
		member.add(memberSpending);	

		// Event Menu - may combine with attendance menu
		event = new JMenu("Event");
		event.setMnemonic('E');
		eventAttendance = createMenuItem("Event Attendance", "", 'A', new ScreenChangeListener("Event Attendance"));
		eventTypes = createMenuItem("Event Types", "", 'A', new ScreenChangeListener("Event Types"));
		event.add(eventAttendance);
		event.add(eventTypes);

		// Reports Menu - contains all the lookups (attendance frequency,
		// targeting, etc)
		report = new JMenu("Report");
		report.setMnemonic('R');
		lottingRights = createMenuItem("Lotting Rights", "", 'L', new ScreenChangeListener("Lotting Rights"));
		report.add(lottingRights);

		// Construct Menu Bar
		menuBar = new JMenuBar();
		menuBar.add(file);
		menuBar.add(member);
		menuBar.add(event);
		menuBar.add(report);
	}

	/**
	 * Assigns actions to the menus
	 */
	private JMenuItem createMenuItem(String label, String toolTip,
			int accelerator, ActionListener action) {
		JMenuItem menu = new JMenuItem();
		menu.setText(label);
		menu.addActionListener(action);
		menu.setMnemonic(accelerator);
		return menu;
	}

	private void layoutComponents() {
		setJMenuBar(menuBar);
		framePanel.add(toolBar, BorderLayout.NORTH);
		framePanel.add(mainPanel, BorderLayout.CENTER);
		getContentPane().add(framePanel);

		setSize(640, 480);

		// Display the window.
		setVisible(true);
	}

	private class ExitListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
	
	/**
	 * Handles navigation to the specified card in the application's CardLayout. 
	 * @author david
	 */
	private class ScreenChangeListener implements ActionListener {
		private String menuToChangeTo;
		
		public ScreenChangeListener(String menuToChangeTo) {
			super();
			this.menuToChangeTo = menuToChangeTo;
		}

		public void actionPerformed(ActionEvent e) {
			cardLayout.show(mainPanel, menuToChangeTo);
			cardLayout.invalidateLayout(mainPanel);
		}
	}
}
